<div id="page-wrapper">
	<div class="row">
		<div class="col-lg-12">
			<h3 class="page-header">
				<span>投票活动</span>
			</h3>
		</div>
		<!-- /.col-lg-12 -->
	</div>
	<!-- /.row -->
	<div class="row">
		<div class="col-lg-12">
			<div class="panel-group" id="accordion">
				<div class="panel panel-default">
					<div class="panel-heading">
						<h4 class="panel-title">
							<a data-toggle="collapse" data-parent="#accordion"
							href="#collapseOne">基本设置
						</a>
					</h4>
				</div>
				<div id="collapseOne" class="panel-collapse collapse in">
					<div class="panel-body">
						<div class="row">
							<div class="col-lg-6">
                                <form role="form" name="praise" method="post" action="{:U('User/praiseeat/update_praise')}" enctype="multipart/form-data">
									<input type="hidden" name="id" value="{$praise['id']}">
									<div class="form-group">
										<label>名称</label>
										<input class="form-control" name="title" value="{$praise['title']}" placeholder="请输入活动名称">
									</div>
									<div class="form-group">
										<label>简介</label>
                                        <!-- <input class="form-control" name="info" value="{$praise['info']}" placeholder="请输入活动简介"> -->
                                        <textarea class="form-control" name="info" rows="5"><?php
                                        	if(isset($praise['info']) && !empty($praise['info'])){
                                        		$data_info = json_decode($praise['info'],true);
                                        		echo $data_info['info'];
                                        	}
                                        ?></textarea>
									</div>
									<div class="form-group">
										<label>活动须知</label>
										<textarea class="form-control" name="keyword"><?php
                                        	if(isset($praise['info']) && !empty($praise['info'])){
                                        		$data_info = json_decode($praise['info'],true);
                                        		echo $data_info['know'];
                                        	}
                                        ?></textarea>
									</div>
                                    <div class="form-group">
                                        <label>图片</label>
                                        <present name="praise.picurl">
											<?php
												$picurlArr = explode(";",$praise['picurl']);
												if(!empty($picurlArr[0])){
												?>
													<img src="<php>echo $picurlArr[0]</php>"  class="thumbnail" width="100%" height="320px" data-preview="<php>echo uploadFile</php>">
													<input type="hidden" name="picurl" value="{$picurlArr[0]}" data-set="<php>echo uploadFile</php>">
													<input type="file" class="changePic" data-action="<php>echo uploadFile</php>"  style="margin-top: -10px;margin-bottom: 10px;">
												<?php }else{ ?>
													<img width="100%" height="200px" src="" alt=" " class="thumbnail preview" data-preview="uploadFile">
													<!-- <input type="hidden" name="picurl" value="{$praise['picurl']}" data-set="uploadFile"> -->
													<input name="picurl" type="file" data-action="uploadFile" style="margin-top: -10px;margin-bottom: 10px;">
											<?php }
											unset($picurlArr[sizeof($picurlArr)-1]);
											for($i = 1; $i < 3; $i++){
												if(isset($picurlArr[$i])){
												?>
											<img src="<php>echo $picurlArr[$i]</php>"  class="thumbnail" width="100%" height="320px" data-preview="<php>echo uploadFile.$i</php>">
											<input type="hidden" name="<php>echo 'picurl'.$i</php>" value="<php>echo $picurlArr[$i]</php>" data-set="<php>echo uploadFile.$i</php>">
											<input type="file" class="changePic" data-action="<php>echo uploadFile.$i</php>" style="margin-top: -10px;margin-bottom: 10px;">
											<?php }else{ ?>
											<img width="100%" height="200px" src="" alt=" " class="thumbnail preview" data-preview="<php>echo uploadFile.$i</php>">
											<!-- <input type="hidden" name="picurl" value="{$praise['picurl']}" data-set="uploadFile"> -->
											<input name="<php>echo 'picurl'.$i</php>" type="file" data-action="<php>echo uploadFile.$i</php>" style="margin-top: -10px;margin-bottom: 10px;">
											<?php }
											} ?>
                                        <else />
                                            <img width="100%" height="200px" src="" alt=" " class="thumbnail preview" data-preview="uploadFile">
                                            <!-- <input type="hidden" name="picurl" value="{$praise['picurl']}" data-set="uploadFile"> -->
                                            <input name="picurl" type="file" data-action="uploadFile" style="margin-top: -10px;margin-bottom: 10px;">
											<img width="100%" height="200px" src="" alt=" " class="thumbnail preview" data-preview="uploadFile1">
											<!-- <input type="hidden" name="picurl" value="{$praise['picurl']}" data-set="uploadFile"> -->
											<input name="picurl1" type="file" data-action="uploadFile1" style="margin-top: -10px;margin-bottom: 10px;">
											<img width="100%" height="200px" src="" alt=" " class="thumbnail preview" data-preview="uploadFile2">
											<!-- <input type="hidden" name="picurl" value="{$praise['picurl']}" data-set="uploadFile"> -->
											<input name="picurl2" type="file" data-action="uploadFile2" style="margin-top: -10px;">
                                        </present>
                                    </div>
                                    <div class="form-group">
                                        <label>每天可以为单幅作品投几票<span style="font-size:12px;color:red;margin-left:5px">＊如若忽略，请填-1</span></label>
										<input class="form-control" name="per_num" value="{$praise['per_num']}" placeholder="">
                                    </div>
                                    <div class="form-group">
                                        <label>活动期间内可以为单幅作品投几票<span style="font-size:12px;color:red;margin-left:5px">＊如若忽略，请填-1</span></label>
										<input class="form-control" name="per_num_sum" value="{$praise['per_num_sum']}" placeholder="">
                                    </div>
                                    <div class="form-group">
                                        <label>每天可以给多少幅作品投票<span style="font-size:12px;color:red;margin-left:5px">＊如若忽略，请填-1</span></label>
										<input class="form-control" name="sum_num" value="{$praise['sum_num']}" placeholder="">
                                    </div>
                                    <div class="form-group">
                                        <label>活动期间总共可以投多少票<span style="font-size:12px;color:red;margin-left:5px">＊如若忽略，请填-1</span></label>
										<input class="form-control" name="limit_sum" value="{$praise['limit_sum']}" placeholder="">
                                    </div>
									<div class="form-group">
										<label>活动开始时间</label>
										<input class="form-control date-picker" name="startdate" value="{$praise['startdate']|date='Y-m-d H:i:s', ###}" placeholder="请输入活动开始时间">
									</div>
									<div class="form-group">
										<label>活动结束时间</label>
										<input class="form-control date-picker" name="enddate" value="{$praise['enddate']|date='Y-m-d H:i:s', ###}" placeholder="请输入活动结束时间">
									</div>
									<div class="form-group">
										<label>是否强制关注</label>
										<input name="showpic" value="0" type="radio" <?php if(empty($praise) || empty($praise['showpic'])){ echo "checked";}?>>否
										<input name="showpic" value="1" type="radio" <?php if(!empty($praise) && !empty($praise['showpic'])){ echo "checked";}?>>是
									</div>
									<button type="submit" class="btn btn-default">保存</button>
								</form>
							</div>
							<!-- /.col-lg-6 (nested) -->
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
</div>
<!-- /#page-wrapper -->
<div class="modal fade netMsgModal" id="netMsgModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">模态框（Modal）标题</h4>
			</div>
			<div class="modal-body">
				<p>在这里添加一些文本</p>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			</div>
		</div><!-- /.modal-content -->
	</div>
</div><!-- /.modal -->
<div class="modal fade progressModal" id="progressMsgModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">模态框（Modal）标题</h4>
			</div>
			<div class="modal-body">
				<p>在这里添加一些文本</p>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
			</div>
		</div><!-- /.modal-content -->
	</div>
</div><!-- /.modal -->

<div class="modal fade progressModal" id="keywordModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">模态框（Modal）标题</h4>
			</div>
			<div class="modal-body">
				<p>在这里添加一些文本</p>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-default sure" >确定</button>
			</div>
		</div><!-- /.modal-content -->
	</div>
</div><!-- /.modal -->
<style type="text/css">
	.blue-font {
		color: #337ec3;
	}
</style>
<!-- <load href="__PUBLIC__/Js/page.user.praise.edit.keyword.js"/> -->
<import type="js" file="common" basepath="/Public/js" />

<script type="text/javascript">
	common.multiSelect.init( "checkboxAll", "checkboxOnce" );
</script>
<!-- <script type="text/javascript">
	//page.user.praise.edit.keyword.init( 6, { table: $("table[name='keyword']"), nav: $("nav[name='keyword']"), modal: $( "#keywordModal" ) } );
</script> -->
<script type="text/javascript">
	var common = common || {};

	common.node = (function(){
		return {
			add: function(){
			},
			drop: function(){
			}
		}
	})(); 

	common.modal = common.modal || {};
	common.modal.netMsgModal = ( function() {

		var modal = undefined;/*$( "#netMsgModal" );*/
		var modalTitle = undefined;/*modal.find( "div.modal-header" ).find( "h4" );*/
		var modalBody  = undefined;/*modal.find( "div.modal-body").find( "p" );*/
		var modalType  = undefined;

		var show = function(){
			$( "#netMsgModal" ).modal( "show" );
		};
		var setTitle = function( title ){
			modalTitle.text( title );//
		};
		var setBody = function( body ){
			modalBody.text( body );//
		};
		var setType = function( cssClass ){
			modalType.addClass( cssClass );//more work left;
		};
		return {
			init: function( options ){
				options.modal && ( modal = options.modal );
				modalTitle = options.modalTitle || modal.find( "div.modal-header" ).find("h4");
				modalBody = options.modalBody || modal.find( "div.modal-body" ).find("p");
			},
			toggle: function( data, hide ){
				data.title && setTitle( data.title );
				data.body  && setBody( data.body );
				data.type  && setType( data.type );
				if( hide ){
					modal.modal( "hide" );
				}else{
					modal.modal( "show" );
				}
			}
		}
	})();

	common.modal.progressModal = ( function(){
		var modal = undefined;
		var modalTitle = undefined;
		var modalBody  = undefined;

		var setTitle = function(){
		};
		var setBody  = function(){
		};

		return {
			init: function( options ){
				options.modal & ( modal = options.modal );
			},
			toggle: function( data, hide ){
				//console.log( "progressModal " + hide );
				console.log( modal );
				if( !hide ){
					modal.modal( "show" );
				}else{
					modal.modal( "hide" );
				}
			}
		}
	})();

	var page = page || {};
	page.user = page.user || {};//TODO: ugly
	page.user.praise = page.user.praise || {};

	page.user.praise.edit = (function(){

		var options_praise_inputs = undefined;
		var options_praise_submit = undefined;

		var options_wxuser_inputs = undefined;
		var options_wxuser_submit = undefined;

		var options_netMsgModal = undefined;
		var options_progressModal = undefined;


		var	event_submit_wxuser = function(){
			options_progressModal.toggle( {}, 0 );
			var formData = {};//要提交的表单数据
			$.each( options_wxuser_inputs, function( key, selector ){
				formData[key] = selector.val();//构建
			} );
			console.log( formData );

			$.ajax({
				url: "/?m=user&c=praiseeat&a=update_wxuser",
				type: 'post',
				data: formData,
				dataType: 'json',//TODO: not secure without php encode;
				success: function( result ){
					console.log( result );
					console.log( result.length );
					if( eval(result).length > 0 ){
						//options_progressModal.toggle( {}, 1 );
						options_netMsgModal.toggle( result );//弹出模态框
					}
				},
				error: function(){
					options_progressModal.toggle( {}, 1 );
					//options_netMsgModal.toggle();//弹出警告模态框
				}
			});
		};

		var event_submit_praise = function(){
			var formData = {};
			$.each( options_praise_inputs, function( key, selector ){
				formData[key] = selector.val();
			} );
			$.ajax( {
				url: "/?m=user&c=praiseeat&a=update_praise",
				type: 'post',
				data: formData,
				dataType: 'json',
				success: function( result ){
					console.log( result );
					if( result.id && result.success ){
						options_praise_inputs.id.val( result.id );
						//options_netMsgModal.toggle( result );
					}
                    if( result.success ){
                        //( result.updated !== undefined ) && result.updated && options_netMsgModal.toggle( { title: "更新", body: "更新成功" } );
                        ( result.updated !== undefined ) && options_netMsgModal.toggle( { title: "更新", body: "更新成功" } );
                        ( result.added !== undefined ) && options_netMsgModal.toggle( { title: "新增", body: "新增成功" } );
                        //( result.saved !== undefined ) && !result.updated && options_netMsgModal.toggle( { title: "新增", body: "新增失败" } );
                    }else{
                        ( result.updated !== undefined ) && ( result.updated === false ) && options_netMsgModal.toggle( { title: "更新", body: "更新失败" } );
                        ( result.updated !== undefined ) && ( result.updated === 0 ) && options_netMsgModal.toggle( { title: "更新", body: "无数据更新" } );
                        //( result.saved !== undefined ) && result.updated && options_netMsgModal.toggle( { title: "新增", body: "新增成功" } );
                        ( result.added !== undefined ) && options_netMsgModal.toggle( { title: "新增", body: "新增失败" } );
                    }
				},
				error: function(){
					//options_netMsgModal.toggle();
				}
			} );
		};

		return {
			init_form_wxuser: function( options ){
				options_wxuser_inputs = options.wxuser_form_inputs;
				//options_wxuser_submit = options.wxuser_form_submit;
				options_wxuser_form = options.wxuser_form;

				options_netMsgModal = options.netMsgModal;
				options_progressModal = options.progressModal;

				options_wxuser_form.submit( function( e ){
					e.preventDefault();//阻止旧的事件;
					event_submit_wxuser();//执行新的事件
				} );
			},

			init_form_praise: function( options ){
				options_praise_inputs = options.praise_form_inputs;
				options_praise_form = options.praise_form;

				options_netMsgModal = options.netMsgModal;
				options_progressModal = options.progressModal;

				options_praise_form.submit( function(e){
					//e.preventDefault();//阻止旧的事件
					//event_submit_praise();//执行新的事件
				} );
			}
		}
	})();

	common.modal.netMsgModal.init( { modal: $("#netMsgModal") } );
	common.modal.progressModal.init( { modal: $("#progressModal") } );

	var wxuser_form_inputs = {
		praise: $( "input[name='praise']" ),
		wxname: $( "input[name='wxname']" ),
		wxid: $( "input[name='wxid']" ),
		weixin: $( "input[name='weixin']" ),
		appid: $( "input[name='appid']"),
		appsecret: $( "input[name='appsecret']" ),
		wechat_type: $( "input[name='wechat_type']" ),
		token: $( "input[name='token']"),
		developerUrl: $( "input[name='developerUrl']" )
	};
	var wxuser_form = $( "form[name='wxuser']" );

	var praise_form_inputs = {
		id: $( "input[name='praise']"),
		title: $( "input[name='title']" ),
		info: $( "input[name='info']" ),
        per_num: $( "input[name='per_num']" ),
        sum_num: $( "input[name='sum_num']" ),
        per_num_sum: $( "input[name='per_num_sum']" ),
        limit: $( "input[name='limit']" ),
		keyword: $( "input[name='keyword']" ),
		startdate: $( "input[name='startdate']" ),
		enddate: $( "input[name='enddate']" ),
		showpic: $("input[name='showpic']")
	};
	var praise_form = $( "form[name='praise']" );

	var options = {};

	options.praise_form = praise_form;
	options.praise_form_inputs = praise_form_inputs;

	options.wxuser_form = wxuser_form;
	options.wxuser_form_inputs = wxuser_form_inputs;

	options.progressModal = common.modal.progressModal;
	options.netMsgModal = common.modal.netMsgModal;

	page.user.praise.edit.init_form_praise( options );
	page.user.praise.edit.init_form_wxuser( options );
</script>
<script type="text/javascript" src="/Public/js/common.upload.js"></script>
<script type="text/javascript">
    common.upload.img.init();
</script>
<script type="text/javascript" src="/Public/js/laydate/laydate.js"></script>
<script type="text/javascript">
$( ".date-picker" ).bind( "click", function(){
	laydate( {
    	format: 'YYYY/MM/DD hh:mm:ss',
    	min: laydate.now(), //设定最小日期为当前日期
    	max: '2099-06-16 23:59:59', //最大日期
    	istime: true,
    	istoday: false
	} );
	laydate.skin( "molv" );
});
</script>
